package framework.utils
{
import framework.core.FontLib;

import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextLineMetrics;

import framework.view.components.button.CommonButton;
import framework.view.components.text.Text;

/**
 * TextUtil
 *
 * @author jliang
 */
public class TextUtil
{
    //==========================================================================
    //  Class methods
    //==========================================================================
    /**
     * Set text to appointed TextField using embed font.
     * @param textFiled the appointed TextField
     * @param text will setting text string.
     * @param font the embed fontName
     *
     */
    public static function setText(textField:TextField, text:String, font:String = null):void
    {
        if(textField == null)
        {
            return;
        }
        var textFormat:TextFormat = textField.getTextFormat();
        textField.embedFonts = true;
        textField.antiAliasType = AntiAliasType.ADVANCED;
        textFormat.font = font ? font : FontLib.UI_FONT_NAME;
		textField.defaultTextFormat = textFormat;
        textField.text = text;
        textField.setTextFormat(textFormat);
		if(textField is Text && font)
		{
			(textField as Text).font = font;
		}
    }
    
	public static function setFixRectText(textField:TextField, text:String, font:String = null, minSize:Number = 10, wrap:Boolean = true):void
	{
		if(textField == null)
		{
			return;
		}
		var maxWidth:Number = textField.width;
		var maxHeight:Number = textField.height;
		var textFormat:TextFormat = textField.defaultTextFormat
		var fontSize:Number = textFormat.size as Number;
		
		var orginalY:Number = textField.y;
		//textField.wordWrap = true;
		textField.embedFonts = true;
		textField.antiAliasType = AntiAliasType.ADVANCED;
		textFormat.font = font ? font : FontLib.UI_FONT_NAME;
		textField.defaultTextFormat = textFormat;
		textField.text = text;
		textField.setTextFormat(textFormat);
		textField.wordWrap = wrap;
		
		while(((textField.textWidth > maxWidth  - 6) ||(textField.textHeight > maxHeight - 4) )&& fontSize > minSize)
		{
			var oldHeight:Number = textField.textHeight;
			fontSize-=1;
			textFormat.size = fontSize;
			textField.text = text;
			textField.setTextFormat(textFormat);
		}
	}
	
    public static function setFilterForTextField(textField:TextField):void
    {
    	var dds:DropShadowFilter = new DropShadowFilter(0, 45, 0x000000, 1, 2, 2, 18, 2);
        textField.filters = [ dds ];
    }

    /**
     * Set htmlText to appointed TextField using embed font.
     * @param textFiled the appointed TextField
     * @param text will setting text string.
     * @param font the embed fontName
     *
     */
    public static function setHtmlText(textField:TextField, htmlText:String):void
    {
        if(textField == null)
        {
            return;
        }
        var textFormat:TextFormat = textField.getTextFormat();
		
		htmlText = htmlText.replace(/&lt;/gms, "<");
		htmlText = htmlText.replace(/&gt;/gms, ">");
		
        textField.htmlText = htmlText;
        textField.embedFonts = true;
        textField.antiAliasType = AntiAliasType.ADVANCED;
        textFormat.font = FontLib.UI_FONT_NAME;

        textField.setTextFormat(textFormat);
    }
    
    public static function setButtonText(button:CommonButton, text:String,
                                         size:int, fillter:Boolean = true,
                                         outColor:uint = 0xFFFFFF,
                                         overColor:uint = 0xFFFFFF,
                                         downColor:uint = 0xFFFFFF):void
    {
        button.textField.embedFonts = true;
        button.textField.antiAliasType = AntiAliasType.ADVANCED;
        button.fontName = FontLib.UI_FONT_NAME;
        button.mouseDownColor = downColor;
        button.rollOutColor = outColor;
        button.rollOverColor = overColor;
        button.fontSize = size;
		button.text = text;
        if (fillter)
        {
        	var glow:GlowFilter = new GlowFilter(0x302E2F, 1, 4, 4, 50);
        	button.textField.filters = [ glow ];
        }
    }
    
    public static function setTextFormatWithOutText(textField:TextField):void
    {
        if(textField == null)
        {
            return;
        }
        var textFormat:TextFormat = textField.getTextFormat();
        textField.embedFonts = true;
        textField.antiAliasType = AntiAliasType.ADVANCED;
        textFormat.font = FontLib.UI_FONT_NAME;

        textField.setTextFormat(textFormat);
    }
    
    /**
     * Set text format to text
     * @param textField A instance of TextField
     * @param size Size of font
     * @param color Color of font
     * @param align Text align of text field
     *
     */
    public static function setTextFormat(textField:TextField,
                                         size:int = 10,
                                         color:uint = 0x000000,
                                         align:String = "left"):void
    {
		var format:TextFormat = new TextFormat(FontLib.UI_FONT_NAME, size,
												color, null, null, null,
												null, null, align);
        textField.defaultTextFormat = format;
		textField.setTextFormat(format);
        textField.selectable = false;
        textField.embedFonts = true;
        textField.antiAliasType = AntiAliasType.ADVANCED;
    }
	
	public static function resizeTextField(tf:TextField):void
	{
		tf.width = tf.textWidth + 4;
		tf.height = tf.textHeight + 4;
	}
    
    public static function isNullOrEmpty(value:String):Boolean
    {
        if (value == null || value == "")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static function replaceHTMLTagToText(value:String):String
    {
        var re:String = value.replace(/</g, "&lt;");
        re = re.replace(/>/g, "&gt;");
        return re;
    }
	//update by yzhou
	public static function subString(str:String):String{
		var _arr:Array = str.split("\\r")
		var _str:String = ""
		for(var i:uint = 0;i < _arr.length;i++){
			_str += _arr[i]+"\r"			
		}
		return _str
	}
	public static function subStringSpace(str:String):String{		
		var _str:String = ""
		for(var i:uint = 0;i < str.length;i++){
			if(str.substr(i,1) != " "){
				_str += str.substr(i,1)		
			} 		
		}
		return _str
	}
	
	public static function getTextRect(target:TextField, size:int):Rectangle
	{
		var rect:Rectangle = new Rectangle();
		
		var tf:TextField = cloneTextField(target);
		
		var format:TextFormat = tf.getTextFormat();
		format.size = size;
		tf.defaultTextFormat = format;
		tf.setTextFormat(format);
		
		resizeTextField(tf);
		
		rect.width = tf.width;
		rect.height = tf.height;
		return rect;
	}
	
	public static function getTextMaxSize(target:TextField, width:Number, height:Number):int
	{
		var tf:TextField = cloneTextField(target);
		tf.autoSize = TextFieldAutoSize.LEFT;
		tf.rotation = 0;
		
		var size:int = 0;
		var flag:Boolean = true;
		while (flag)
		{
			var format:TextFormat = tf.defaultTextFormat;
			size = format.size as int;
			format.size += 1;
			if (size == 127)
			{
				flag = false;
			}
			else
			{
				tf.defaultTextFormat = format;
				tf.setTextFormat(format);
				
				var w:Number = tf.textWidth * Math.cos(45);
				
				if (w > width)
				{
					flag = false;
				}
			}
			
		}
		
		return size;
	}
	
	public static function cloneTextField(target:TextField):TextField
	{
		var tf:TextField = new TextField();
		var temp:TextFormat = target.getTextFormat();
		var format:TextFormat = new TextFormat();
		format.align = temp.align;
		format.blockIndent = temp.blockIndent;
		format.bold = temp.bold;
		format.bullet = temp.bullet;
		format.color = temp.color;
		format.display = temp.display;
		format.font = temp.font;
		format.indent = temp.indent;
		format.italic = temp.italic;
		format.kerning = temp.kerning;
		format.leading = temp.leading;
		format.leftMargin = temp.leftMargin;
		format.letterSpacing = temp.letterSpacing;
		format.rightMargin = temp.rightMargin;
		format.size = temp.size;
		format.tabStops = temp.tabStops;
		format.target = temp.target;
		format.underline = temp.underline;
		format.url = temp.url;
		
		tf.antiAliasType = target.antiAliasType;
		tf.autoSize = target.autoSize;
		tf.border = target.border;
		tf.borderColor = target.borderColor;
		tf.multiline = target.multiline;
		tf.wordWrap = target.wordWrap;
		tf.embedFonts = target.embedFonts;
		
		tf.scaleX = target.scaleX;
		tf.scaleY = target.scaleY;
		tf.rotation = target.rotation;
		
		tf.defaultTextFormat = format;
		tf.text = target.text;
		tf.setTextFormat(format);
		
		return tf;
	}
}
}



